home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / mac / Shared / Freeman / TravSalP.m < prev   
Text File  |  1993-10-27  |  3KB  |  83 lines

  1. BeginPackage["TravelingSalesperson`"]
  2.  
  3. nOutOfN::usage = "nOutOfN[weights,externIn,numUnits,lambda,deltaT,
  4. numIters,printFreq,reset:False]"
  5. tsp::usage = "tsp[weights,externIn,numUnits,lambda,deltaT,
  6. numIters,printFreq,reset:False]"
  7.  
  8. Begin["`Private`"]    (* begin the private context *)
  9.  
  10. nOutOfN[weights_,externIn_,numUnits_,lambda_,deltaT_,
  11. numIters_,printFreq_,reset_:False]:=
  12.     Module[{iter,l,dt,indx,ins},
  13.       dt=deltaT;
  14.       l=lambda;
  15.       iter=numIters;
  16.       ins=externIn;
  17.               (* only reset if starting over *)
  18.       If[reset,ui=Table[Random[],{numUnits}];
  19.                  vi = g[l,ui],Continue];  (* end of If *)
  20.           Print["initial ui = ",N[ui,2]];Print[];
  21.           Print["initial vi = ",N[vi,2]];
  22.       For[iter=1,iter<=numIters,iter++,
  23.         indx = Random[Integer,{1,numUnits}];
  24.         ui[[indx]] = ui[[indx]]+
  25.             dt (vi . Transpose[weights[[indx]]] +
  26.             ui[[indx]] + ins[[indx]]);
  27.         vi[[indx]] = g[l,ui[[indx]]];
  28.         If[Mod[iter,printFreq]==0,
  29.             Print[];Print["iteration = ",iter];
  30.             Print["net inputs = "];
  31.             Print[N[ui,2]];
  32.             Print["outputs = "];
  33.             Print[N[vi,2]];Print[];
  34.             ];  (* end of If *)
  35.         ];  (* end of For *)
  36.         Print[];Print["iteration = ",--iter];
  37.         Print["final outputs = "];
  38.         Print[vi];
  39.        ];  (* end of Module *)
  40.  
  41.  
  42.  
  43. tsp[weights_,externIn_,numUnits_,lambda_,deltaT_,
  44. numIters_,printFreq_,reset_:False]:=
  45.     Module[{iter,l,dt,indx,ins,utemp},
  46.       dt=deltaT;
  47.       l=lambda;
  48.       iter=numIters;
  49.       ins=externIn;
  50.               (* only reset if starting over *)
  51.       If[reset,
  52.          utemp = ArcTanh[(2.0/Sqrt[numUnits])-1]/l;
  53.          ui=Table[
  54.            utemp+Random[Real,{-utemp/10,utemp/10}],
  55.                    {numUnits}];  (* end of Table *)
  56.                  vi = g[l,ui],Continue];  (* end of If *)
  57.           Print["initial ui = ",N[ui,2]];Print[];
  58.           Print["initial vi = ",N[vi,2]];
  59.       For[iter=1,iter<=numIters,iter++,
  60.         indx = Random[Integer,{1,numUnits}];
  61.         ui[[indx]] = ui[[indx]]+
  62.             dt (vi . Transpose[weights[[indx]]] +
  63.             ui[[indx]] + ins[[indx]]);
  64.         vi[[indx]] = g[l,ui[[indx]]];
  65.         If[Mod[iter,printFreq]==0,
  66.             Print[];Print["iteration = ",iter];
  67.             Print["net inputs = "];
  68.             Print[N[ui,2]];
  69.             Print["outputs = "];
  70.             Print[N[vi,2]];Print[];
  71.             ];  (* end of If *)
  72.         ];  (* end of For *)
  73.         Print[];Print["iteration = ",--iter];
  74.         Print["final outputs = "];
  75.         Print[MatrixForm[Partition[N[vi,2],Sqrt[numUnits]]]];
  76.        ];  (* end of Module *)
  77.  
  78.  
  79.  
  80.  
  81. End[]         (* end the private context *)
  82.  
  83. EndPackage[]  (* end the package context *)